'use strict';
/**
 * Created by FreezeSoul on 2015/9/28.
 */
define([
        'angular',
        'app/modules/source/define',
        'app/modules/source/controllers/source-edit',
        'app/modules/source/controllers/source-delete',
        'app/modules/source/services/source'
    ],
    function(angular) {
        angular.module('ad.source')
            .controller('adSourceController',['$scope','$uibModal','_','Notification','adSourceService',function($scope,$uibModal,_,Notification,service) {
                $scope.sources = [];
                $scope.isLoading = false;
                $scope.searchWord = "";

                $scope.maxSize = 5;
                $scope.itemsPerPage = adConfig.perPageRowSize;
                $scope.currentPage = 1;
                $scope.totalItems = 0;

                $scope.searchData = function(){
                    $scope.isLoading = true;
                    service.getPage($scope.currentPage, $scope.itemsPerPage, $scope.searchWord).then(function (data) {
                        var obj = angular.fromJson(data);
                        if(obj.result == 'success'){
                            $scope.sources = obj.data.items;
                            $scope.totalItems = obj.data.count;
                        }
                        $scope.isLoading = false;
                    });
                };

                $scope.pageChanged = function () {
                    $scope.searchData();
                };

                $scope.newData = function () {
                    var modalInstance = $uibModal.open({
                        animation: true,
                        templateUrl: 'app/modules/source/views/source-edit.tpl.html',
                        controller: 'adSourceEditController',
                        size: 'lg',
                        resolve: {
                            item: function () {
                                return {datasource_id:''};
                            }
                        }
                    });

                    modalInstance.result.then(function (data) {
                        var obj = angular.fromJson(data);
                        if(obj.result == 'success') {
                            Notification.success('添加成功！');
                        }
                        $scope.searchData();
                    }, function () {
                    });
                };

                var deleteItems = function(items){
                    var modalInstance = $uibModal.open({
                        animation: true,
                        templateUrl: 'app/modules/source/views/source-delete.tpl.html',
                        controller: 'adSourceDeleteController',
                        size: 'sm',
                        resolve: {
                            items: function () {
                                return items;
                            }
                        }
                    });

                    modalInstance.result.then(function (data) {
                        var obj = angular.fromJson(data);
                        if(obj.result == 'success') {
                            Notification.success('删除成功！');
                        }
                        $scope.searchData();
                    }, function () {
                    });
                }

                $scope.deleteSelected = function () {
                    var items =  _.filter($scope.sources, function(source){ return source.isSelected; });
                    deleteItems(items);
                };

                $scope.deleteRow = function (row) {
                    deleteItems([row]);
                };

                $scope.editRow = function (row) {
                    var modalInstance = $uibModal.open({
                        animation: true,
                        templateUrl: 'app/modules/source/views/source-edit.tpl.html',
                        controller: 'adSourceEditController',
                        size: 'lg',
                        resolve: {
                            item: function () {
                                return row;
                            }
                        }
                    });

                    modalInstance.result.then(function (data) {
                        var obj = angular.fromJson(data);
                        if(obj.result == 'success') {
                            Notification.success('修改成功！');
                        }
                    }, function () {
                    });
                };

                $scope.searchData();
            }]);
    });